<?php


namespace app\common\library;


use common;
use think\facade\Log;

class  BaiDuMapModel
{
    var $ak = 'd0j755XAhyRP8SpgqslP5KfSnNcTdDHV';

    function code_to_msg($code){
        $code_eror_msg = [
            1 => '内部错误',
            4 => '转换失败',
            21 => 'from非法',
            22 => 'to非法',
            24 => 'coords格式非法',
            25 => 'coords个数非法，超过限制',
            26 => '参数错误',
            0 => 'OK',
        ];

        if (isset($code_eror_msg[$code])) {
            return $code_eror_msg[$code];
        }

        return "error";
    }

    function wgs_to_baidu($coordinate){
        $url = "http://api.map.baidu.com/geoconv/v1/?coords={$coordinate[0]},{$coordinate[1]}&from=1&to=6&ak={$this->ak}";
        $result = common::curlget($url, null);
        $j = null;
        if ($result) {
            $j = json_decode($result, true);
        }

        if (!empty($j)){
            if ($j['status'] == 0){
                return $j['result'][0];
            }
            else
            {
                Log::error("status error".$this->code_to_msg($j['status']));
            }
        }

        $ret =[
            "x" =>0,
            "y" =>0,
        ];
        return $ret;
    }

}